package com.example.jegotrip.fragment.home;

import static android.view.View.GONE;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.activity.CreateNoteActivity;
import com.example.jegotrip.activity.NoteDetailsActivity;
import com.example.jegotrip.adapter.NoteListAdapter;
import com.example.jegotrip.base.BaseFragment;
import com.example.jegotrip.db.NoteDBHelper;
import com.example.jegotrip.entity.NoteInfo;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomToast;

import java.util.List;

public class HomeFragment extends BaseFragment {
    private UserInfo userInfo;
    private List<NoteInfo> noteInfos;
    private RecyclerView recyclerView;
//    适配器
    private NoteListAdapter noteListAdapter;
    private String[] item1 = new String[]{"置顶","编辑","删除"};
    private String[] item2 = new String[]{"取消置顶","编辑","删除"};
    private int currentIndex = 0;

    private LinearLayoutCompat ll_empty;

    @Override
    protected void initData() {
//        加载记事本数据
        userInfo = UserInfo.getUserInfo();
        noteInfos = NoteDBHelper.getInstance(getActivity()).queryNotes(userInfo.getUsername());
//        初始化适配器
        noteListAdapter = new NoteListAdapter(noteInfos);
        if (noteInfos.isEmpty()){
            ll_empty.setVisibility(View.VISIBLE);
        } else {
            ll_empty.setVisibility(GONE);
        }

//        设置适配器
        recyclerView.setAdapter(noteListAdapter);

//        recyclerView点击事件
        noteListAdapter.setmNoteListOnItemClickListener(new NoteListAdapter.NoteListOnItemClickListener() {
            @Override
            public void onItemClick(int position, NoteInfo noteInfo) {
                Intent intent = new Intent(new Intent(getActivity(), NoteDetailsActivity.class));
                intent.putExtra("noteInfo", noteInfo);
                startActivity(intent);
            }

            @Override
            public void onItemMoreClick(int position, NoteInfo noteInfo) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("操作")
                        .setSingleChoiceItems(noteInfo.getNote_is_top() == 0 ? item1 : item2, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                currentIndex = i;
                            }
                        })
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                if (currentIndex == 0){// 置顶、取消置顶
                                    setNoteIsTop(noteInfo);

                                } else if (currentIndex == 1){// 编辑
                                    setNoteIsTop(noteInfo);
                                    Intent intent = new Intent(new Intent(getActivity(), CreateNoteActivity.class));
                                    intent.putExtra("noteInfo", noteInfo);
                                    startActivity(intent);
                                } else if (currentIndex == 2){
                                    int res = NoteDBHelper.getInstance(getActivity()).deleteNote(noteInfo.getNote_id());
                                    if (res > 0){
                                        CustomToast.showCenterToast(getActivity(),"删除成功");
                                        initData();
                                    } else {
                                        CustomToast.showCenterToast(getActivity(),"操作失败");

                                    }
                                }
                            }
                        })
                        .show();
            }
        });
    }

    /**
     * 是否置顶
     * @param noteInfo
     */
    private void setNoteIsTop(NoteInfo noteInfo){
        // 未置顶，将记事本记录置顶
        int res = NoteDBHelper.getInstance(getActivity()).isTopNote(noteInfo);
        if (res > 0){
            CustomToast.showCenterToast(getActivity(),(noteInfo.getNote_is_top() == 0 ? item1[0] : item2[0])+"成功");
            initData();
        } else {
            CustomToast.showCenterToast(getActivity(),"操作失败");

        }

    }

    @Override
    protected void setListener() {
        rootView.findViewById(R.id.btn_note_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CreateNoteActivity.class);
                startActivityForResult(intent, 1000);// 跳转并刷新数据需要用startActivityForResult
            }
        });
    }

    @Override
    protected void initViews() {
        recyclerView = rootView.findViewById(R.id.recyclerView);

        ll_empty = rootView.findViewById(R.id.ll_empty);
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.fragment_home;
    }

    /**
     * 创建备忘录成功后在此写回调
     *
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        if (resultCode == 1000){
            initData();// 刷新数据
        }
    }
}
